//
//  CareerSelect.m
//  thing
//
//  Created by HaiJiao Chen on 12-11-16.
//  Copyright (c) 2012年 Kaixin. All rights reserved.
//

#import "PrinterSelect.h"


@implementation PrinterSelect
@synthesize delegate;
@synthesize printer;
@synthesize printers;

- (void)dealloc
{
    [printer release];
    [printers release];
    [picker release];
    [inputView release];
    [super dealloc];
}

- (UIView*)inputView
{
    if (inputView == nil)
    {
        inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 1024, 1024, 216+41)];
        
        picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 41, 1024, 416)];
        picker.delegate = self;
        picker.dataSource = self;
        [inputView addSubview:picker];
        picker.showsSelectionIndicator = YES;
        
        UIImageView* bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 41)];
        bg.image = [UIImage imageNamed:@"keyboard_botton._bg@2x.png"];
        [inputView addSubview:bg];
        [bg release];
        
        UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(1024-10-50, 6, 50, 30);
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(returnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [inputView addSubview:button];
    }
    return inputView;
}
-(BOOL)canBecomeFirstResponder
{
    BOOL can = YES;
    if (delegate && [delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)])
    {
        can = [delegate textFieldShouldBeginEditing:self];
    }
    if (can)
    {
        if (printer)
        {
            NSUInteger index = [printers indexOfObject:printer];
            [picker selectRow:index inComponent:0 animated:NO];
        }
    }
    return can;
}

- (BOOL)canResignFirstResponder
{
    BOOL can = YES;
    if (delegate && [delegate respondsToSelector:@selector(textFieldShouldEndEditing:)])
    {
        can = [delegate textFieldShouldEndEditing:self];
    }
    return can;
}

- (void)returnClicked:(id)sender
{
    BOOL can = YES;
    if (delegate && [delegate respondsToSelector:@selector(textFieldShouldReturn:)])
    {
        can = [delegate textFieldShouldReturn:self];
    }
    if (can)
    {
        int row = [picker selectedRowInComponent:0];
        if (row >= 0 && row < [printers count])
        {
            self.printer = [printers objectAtIndex:[picker selectedRowInComponent:0]];
        }
        else
        {
            self.printer = nil;
        }
        self.text = self.printer;
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self becomeFirstResponder];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void)setPrinter:(NSString *)_printer
{
    [printer release];
    printer = [_printer copy];
    if (printer == nil)
    {
        self.text = nil;
    }
    else
    {
        self.text = printer;
        NSUInteger index = [printers indexOfObject:printers];
        [picker selectRow:index inComponent:0 animated:NO];
    }
}
-(NSString*)selectPrinter
{
    int row = [picker selectedRowInComponent:0];
    if (row >= 0 && row < [printers count])
    {
        return [printers objectAtIndex:[picker selectedRowInComponent:0]];
    }
    return nil;
}

//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
//{
//}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [printers objectAtIndex:row];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [printers count];
}

@end
